package com.yicong.ants.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yicong.ants.R;

/**
 * 通用的标题栏
 */
public class CustomToolBar extends LinearLayout {
    private final View view;
    private CallBack callBack;
    private ConfirmFace confirmBack;

    public CustomToolBar(Context context) {
        this(context, null);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomToolBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomToolBar);
        String title = typedArray.getString(R.styleable.CustomToolBar_title_txt);
        String confirm = typedArray.getString(R.styleable.CustomToolBar_confirm_txt);
        boolean isShow = typedArray.getBoolean(R.styleable.CustomToolBar_show_back, true);
        boolean fullScreen = typedArray.getBoolean(R.styleable.CustomToolBar_full_screen, false);
        if (fullScreen) {
            view = LayoutInflater.from(context).inflate(R.layout.view_full_screen_tool_bar, this);
        } else {
            view = LayoutInflater.from(context).inflate(R.layout.view_custom_tool_bar, this);
        }
        TextView titleView = view.findViewById(R.id.tv_title);
        TextView confirmView = view.findViewById(R.id.tv_confirm);
        ImageView imageView = view.findViewById(R.id.iv_back);
        titleView.setText(null == title ? "" : title);
        confirmView.setText(null == confirm ? "" : confirm);
        if (isShow) {
            imageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (null != callBack) {
                        callBack.callBack();
                    }
                }
            });
        } else {
            imageView.setVisibility(INVISIBLE);
        }
        confirmView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (null != confirmBack) {
                    confirmBack.callBack();
                }
            }
        });
        typedArray.recycle();
    }

    public void setConfirmFace(ConfirmFace confirmBack) {
        this.confirmBack = confirmBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    public void setTitleTxt(String title) {
        if (null != view && null != title) {
            TextView textView = view.findViewById(R.id.tv_title);
            if (null != textView) {
                textView.setText(title);
            }
        }
    }

    /**
     * 确定按钮回调
     */
    public interface ConfirmFace {
        void callBack();
    }

    /**
     * 返回按钮回调
     */
    public interface CallBack {
        void callBack();
    }
}
